Load Libraries

Tidy Data

Build your plots layer by layer

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y=lifeExp))
p + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
Life Expectancy vs GDP, using a smoother.

Life Expectancy vs GDP, using a smoother.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y=lifeExp))
p + geom_point() + geom_smooth() 
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
Life Expectancy vs GDP, showing both points and a GAM smoother.

Life Expectancy vs GDP, showing both points and a GAM smoother.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y=lifeExp))
p + geom_point() + geom_smooth(method = "lm") 
## `geom_smooth()` using formula 'y ~ x'
Life Expectancy vs GDP, points and an ill-advised linear fit.

Life Expectancy vs GDP, points and an ill-advised linear fit.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y=lifeExp))
p + geom_point() +
    geom_smooth(method = "gam") +
    scale_x_log10()
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
Life Expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis.

Life Expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis.

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp))
p + geom_point() +
    geom_smooth(method = "gam") +
    scale_x_log10(labels = scales::dollar)
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
Life Expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis, with better labels on the tick marks.

Life Expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis, with better labels on the tick marks.

Mapping aesthetics vs setting them

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = "purple"))
p + geom_point() +
    geom_smooth(method = "loess") +
    scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
What has gone wrong here?

What has gone wrong here?

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp))
p + geom_point(color = "purple") +
    geom_smooth(method = "loess") +
    scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Setting the color attribute of the points directly.

Setting the color attribute of the points directly.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) 
p + geom_point(alpha = 0.3) +
    geom_smooth(color = "orange", se = FALSE, size = 8, method = "lm") +
    scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Setting some other arguments.

Setting some other arguments.

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp))
p + geom_point(alpha = 0.3) + geom_smooth(method = "gam") +
    scale_x_log10(labels = scales::dollar) +
    labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
         title = "Economic Growth and Life Expectancy",
         subtitle = "Data points are country-years",
         caption = "Source: Gapminder.")
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
A more polished plot of Life Expectancy vs GDP.

A more polished plot of Life Expectancy vs GDP.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = continent))
p + geom_point() +
    geom_smooth(method = "loess") +
    scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Mapping the continent variable to the color aesthetic.

Mapping the continent variable to the color aesthetic.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = continent,
                          fill = continent))
p + geom_point() +
    geom_smooth(method = "loess") +
    scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Mapping the continent variable to the color aesthetic, and correcting the error bars using the fill aesthetic.

Mapping the continent variable to the color aesthetic, and correcting the error bars using the fill aesthetic.

Aesthetics can be mapped per geom

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = continent)) +
    geom_smooth(method = "loess") +
    scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Mapping aesthetics on a per-geom basis. Here color is mapped to continent for the points but not the smoother.

Mapping aesthetics on a per-geom basis. Here color is mapped to continent for the points but not the smoother.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp))
p + geom_point(mapping = aes(color = log(pop))) +
    scale_x_log10()    
Mapping a continuous variable to color.

Mapping a continuous variable to color.

Save your work

knitr::opts_chunk$set(fig.width=8, fig.height=5) 
ggsave(filename = "figures/my_figure.png")
here() 
## [1] "/Users/zhuhadar/R-workspace/Lily R Data Vis/LilyFork_kjhealy/stathorizons_0820"
p_out <- p + geom_point(mapping = aes(color = log(pop))) +
    scale_x_log10()

ggsave(here("figures", "lifexp_vs_gdp_gradient.pdf"), plot = p_out)
## Saving 7 x 5 in image
ggsave(here("figures", "lifexp_vs_gdp_gradient.png"), plot = p_out)
## Saving 7 x 5 in image